<template>
    <div>
      <cfg-table
        ref="table"
        :columns="columns"
        :data="data"
        :loading="loading"
        :config="config"
        :pageSizeOpts="pageSizeOpts"
        :pageNavigation="pageNavigation"
        :queryFormItem="queryFormItem"
        :collapseFormItem="collapseFormItem"
        :additionalAction="additionalAction"
        @loadPage="loadPage"
        @handleCreate="handleCreate"
        @handleDetail="handleDetail"
        @handleEdit="handleEdit"
        @handleDelete="handleDelete"
        @handleDeleteMultiple="handleDeleteMultiple"
        @handleSearch="handleSearch"
        @handleReset="handleReset"
      ></cfg-table>
      <new-add-modal
        v-bind="addNewPanel"
        :modelType="modelType"
        :columns="formColumns"
        :rules="rules"
        @submit="handleNewAddSubmit"
      ></new-add-modal>
      <edit-modal
        v-bind="editPanel"
        :modelType="modelType"
        :columns="formColumns"
        :rules="rules"
        @submit="handleEditSubmit"
      ></edit-modal>
      <read-modal
        v-bind="readPanel"
        :modelType="modelType"
        :columns="columns"
        :rules="rules"
      ></read-modal>
      <!-- 绑定质检项 -->
      <select-modal-opt
      v-bind="selectPanel"
      :modelType="selectModelType"
      :type="selectType"
      :className="selectClassName"
      :columns="selectColumns"
      :moreColumn="selectMormColumns"
      :formColumns="selectFormColumns"
      :isSingleCheck="isSingleCheck"
      :selectionData="selectionData"
      @submit="handleSelectSubmit"
      ></select-modal-opt>
    </div>
</template>

  <script>
    import newAddModal from '@/pages/components/newAddModal.vue';
    import editModal from '@/pages/components/editModal.vue';
    import readModal from '@/pages/components/readModal.vue';
    import selectModalOpt from '@/pages/components/selectModalOpt';
    import { typeClassName } from '../../config/classNameMap';
    import mixin from '@/pages/components/mixins/mixin'
    import columnsMixin from './config'
    export default {
        components: { newAddModal, editModal, readModal, selectModalOpt },
        name: 'qualityTemplate',
        mixins: [mixin,columnsMixin],
        data () {
            return {
                classObject: null, // 单个实例
                collectionObject: null, // 所有实例
                className: typeClassName['qualityTemplate'].className,
                type: typeClassName['qualityTemplate'].type,
                modelType: typeClassName['qualityTemplate'].modelType,
                selectModelType: typeClassName['qualityItem'].modelType, // 选择质检项
                selectType: typeClassName['qualityItem'].type, // 质检项type
                selectClassName: typeClassName['qualityItem'].className, // 质检项className
                isSingleCheck: false,
                selectionData: [], // 质检项
                addNewPanel: {
                    width: 1024,
                    zIndex: 100,
                    titleStyle: {},
                    drawer: {
                        show: false
                    },
                    cols: 2
                },
                editPanel: {
                    width: 1024,
                    zIndex: 100,
                    titleStyle: {},
                    drawer: {
                        show: false
                    },
                    cols: 2,
                    data: {}
                },
                readPanel: {
                    width: 1024,
                    zIndex: 100,
                    titleStyle: {},
                    drawer: {
                        show: false
                    },
                    cols: 2,
                    data: {}
                },
                selectPanel: {
                    width: 1024,
                    zIndex: 100,
                    titleStyle: {},
                    drawer: {
                        show: false
                    },
                    cols: 2,
                    data: {}
                },
                rules: {
                    sf_code: [
                        { required: true, message: '不能为空', trigger: 'blur' }
                    ],
                    sf_name: [
                        { required: true, message: '不能为空', trigger: 'blur' }
                    ]
                },
                loading: false,
                config: {
                    search: true,
                    addButton: true,
                    batchDeleteButton: true,
                    tableSizeIcon: true,
                    fullscreenIcon: true,
                    refreshIcon: false,
                    columnSettingIcon: true,
                    action: true,
                    additionalAction: true
                },
                pageSizeOpts: [10, 30, 50, 100],
                pageNavigation: { total: 0, pageSize: 10, pageNumber: 1 }
            };
        },
        computed: {
            additionalAction () {
                let additionalAction =  {
                    key: 'additionalAction',
                    title: {
                        'zh-CN': '附加操作',
                        'en-US': 'additional action'
                    }[this.G_Locale],
                    slot: 'additionalAction',
                    width: 200,
                    fixed: 'right',
                    align: 'center',
                    show: true,
                    buttons: [
                        { title: {
                              'zh-CN': '绑定质检项',
                              'en-US': 'bind quality item'
                          }[this.G_Locale],
                          event: (row, index) => this.handleQualityItem(row, index)
                        }
                    ]
                }
                this.G_IsMobile && (additionalAction.width = 90)
                return additionalAction;
            },
            data () {
                if (!!this.collectionObject) {
                    return this.collectionObject.records.map(d => {
                        return {
                            ...d,
                            ...d.values
                        }
                    })
                } else {
                    return [];
                }
            }
        },
        beforeMount () {
            this.collectionObject = this.$modelObject.create(`${this.modelType}Collection`);
            this.classObject = this.$modelObject.create(this.modelType);
            this.loadPage(this.pageNavigation, null);
        },
        mounted () {},
        methods: {
            // 绑定质检项弹框
            async handleQualityItem (row, index) {
                this.loading = true;
                if (await this.getRecord({ type: row.type, className: row.className, id: row.id })) {
                    this.selectionData = this.getSelectionByHierClassName(typeClassName['qualityItem'].className)
                    this.$set(this.selectPanel.drawer, 'show', true)
                    this.loading = false;
                } else {
                    this.loading = false;
                }
            },
            // 绑定质检项确定
            handleSelectSubmit (selection) {
                let pointer = { className: this.className, type: this.type, id: this.classObject.record.id }
                let newData = selection.map(d => {
                    return {
                        values: { sf_name: d.label, id: d.id },
                        name: 'qualityItem-' + String(d.id),
                        id: d.id,
                        model: typeClassName['qualityItem'].modelType,
                        pointer: { id: d.id, className: typeClassName['qualityItem'].className, type: typeClassName['qualityItem'].type }
                    }
                })
                let originalData = [];
                if (!!this.classObject.hier && this.classObject.hier.length > 0) {
                    this.classObject.hier[0].basic.forEach(item => {
                        if (item.pointer.className !== typeClassName['qualityItem'].className) {
                            originalData.push({ ...item })
                        }
                    })
                }
                newData = [...newData, ...originalData]
                this.hierAllAddRegister(pointer, newData, this.selectPanel)
            }
        }
    };
  </script>
  <style scoped lang="scss"></style>
